const { getRepairItemAPI, addRepairItemAPI, getRepairDetailAPI } = require("../../../apis/repair")
const { getRoomListAPI } = require("../../../apis/room")
const { baseURL } = require("../../../utils/request")

Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseInfo: '',
    repairItemName: '',
    houseList: [],
    appointment: '',
    repairItemId: '',
    repairItem: [],
    attachment: [],
    mobile: '',
    description: '',
    houseId: ''
  },
  async onLoad(opts) {
    if (opts.id) {
      const res = await getRepairDetailAPI(opts.id)
      const { repairItemId, houseInfo, appointment, houseId, repairItemName, mobile, description, attachment, status, id } = res.data
      this.setData({
        id,
        repairItemId,
        houseInfo,
        appointment,
        houseId,
        repairItemName,
        mobile,
        description,
        attachment,
        status
      })
    }
    this.getRepairItem()
    this.getBuildingList()
  },
  selectDate(ev) {
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(ev.detail),
    })
  },
  async getBuildingList() {
    const res = await getRoomListAPI()
    const houseList = res.data.map(item => ({ name: item.point + ' ' + item.building + ' ' + item.room, id: item.id }))
    this.setData({ houseList })
  },
  async getRepairItem() {
    const res = await getRepairItemAPI()
    this.setData({
      repairItem: res.data,
    })
  },
  afterRead(ev) {
    const { file } = ev.detail
    wx.uploadFile({
      url: baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${wx.getStorageSync('token')}`,
      },
      success: (res) => {
        // 转换 json 数据
        const data = JSON.parse(res.data)
        // 上传完成需要更新
        const { attachment = [] } = this.data
        attachment.push({ ...data.data })
        // 渲染数据
        this.setData({ attachment })
      },
    })
  },
  selectRepairItem(ev) {
    // 获取用户选择的维修项目名称
    const { name: repairItemName, id: repairItemId } = ev.detail
    // 页面中渲染
    this.setData({ repairItemName, repairItemId })
  },
  selectHouse(ev) {
    // 获取用户选择房屋的名称
    const { id: houseId, name: houseInfo } = ev.detail
    // 页面中渲染
    this.setData({ houseId, houseInfo })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  verifyHouse() {
    const valid = this.data.houseId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  verifyRepair() {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  verifyMobile() {
    // 验证手机号
    const reg = /^[1][3-8][0-9]{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate() {
    // 验证日期格式
    const reg = /^\d{4}-\d{1,2}-\d{1,2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription() {
    // 验证报修项目描述
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  async submitForm() {
    // 逐个验证表单数据
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return

    const { houseId, repairItemId, appointment, mobile, description, attachment, id } = this.data
    const { code } = addRepairItemAPI({
      houseId, repairItemId, appointment, mobile, description, attachment, id
    })
    if (code == 10000) return wx.showToast({ title: '在线报修失败!', icon: 'none' })
    // 跳转到维修列表页面
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
